package com.example.restatr.Atcivity.Park;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.button.MaterialButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;

public class AddMileageAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private TextView date;
    private EditText distance;
    private EditText gasFilling;
    private EditText amount;
    private MaterialButton mileageCommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_mileage);
        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        date = findViewById(R.id.date);
        distance = findViewById(R.id.Distance);
        gasFilling = findViewById(R.id.gasFilling);
        amount = findViewById(R.id.amount);
        mileageCommit = findViewById(R.id.mileageCommit);
        Tools.SetBarTitle(tb, barTitle, "添加里程信息", search, AddMileageAt.this, false);
        if (getIntent().getStringExtra("date") != null) {
            date.setText(getIntent().getStringExtra("date"));
            distance.setText(getIntent().getStringExtra("distance"));
            gasFilling.setText(getIntent().getStringExtra("gasFill"));
            amount.setText(getIntent().getStringExtra("amount"));
            mileageCommit.setText("修改");
            Tools.SetBarTitle(tb, barTitle, "修改里程信息", search, AddMileageAt.this, false);
        }
    }

    //添加里程信息
    private void PutMileage(String Date, String Distance, String GasFilling, String Amount) {
        new Thread(() -> {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("amount", Amount);
                jsonObject.put("gasFilling", GasFilling);
                jsonObject.put("plateNo", getIntent().getStringExtra("plateNo"));
                jsonObject.put("travelDate", Date);
                jsonObject.put("travelDistance", Distance);
                String post = Tools.Post("/prod-api/api/park/car/consumption", MainActivity.sp.getString("token", ""), jsonObject.toString());
                JSONObject jx = new JSONObject(post);
                String msg = jx.getString("msg");
                int code = jx.getInt("code");
                runOnUiThread(() -> {
                    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                    if (code == 200) {
                        finish();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }

    //修改里程信息
    private void AmendMileage(String Date, String Distance, String GasFilling, String Amount) {
        new Thread(() -> {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("id", getIntent().getStringExtra("id"));
                jsonObject.put("amount", Amount);
                jsonObject.put("gasFilling", GasFilling);
                jsonObject.put("travelDate", Date);
                jsonObject.put("travelDistance", Distance);
                String token = Tools.Put("/prod-api/api/park/car/consumption", MainActivity.sp.getString("token", ""), jsonObject.toString());
                JSONObject jx = new JSONObject(token);
                String msg = jx.getString("msg");
                int code = jx.getInt("code");
                runOnUiThread(() -> {
                    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                    if (code == 200) {
                        finish();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mileageCommit:
                if (Tools.isEmptyText(date) && Tools.isEmptyText(distance) && Tools.isEmptyText(gasFilling) && Tools.isEmptyText(amount)) {
                    if (mileageCommit.getText().toString().equals("修改")) {
                        AmendMileage(date.getText().toString(), distance.getText().toString(), gasFilling.getText().toString(), amount.getText().toString());
                    } else {
                        PutMileage(date.getText().toString(), distance.getText().toString(), gasFilling.getText().toString(), amount.getText().toString());
                    }
                } else {
                    Toast.makeText(this, "不能为空！", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.date:
                DatePickerDialog datePickerDialog = new DatePickerDialog(this);
                datePickerDialog.create();
                datePickerDialog.show();
                datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        month+=1;
                        date.setText(year + "-" + month + "-" + dayOfMonth);
                        datePickerDialog.dismiss();
                    }
                });
                break;
        }
    }
}